import bb.cascades 1.3
import bb.device 1.2
import bb.system 1.2
import bb.platform 1.2

Container {
    property alias titleStyle: titleText.style
    property alias descStyle: descText.style
    property alias typeStyle: typeText.style
    property int screenWidth: 720
    property int screenHeight: 720
    property alias hardwareInfo: hardwareinfo
    property alias platForm: platform
    
    attachedObjects: [
        HardwareInfo {
            id: hardwareinfo
        },
        
        PlatformInfo {
            id: platform
        },
        
        TextStyleDefinition {
            id: descText
            color: Color.create("#FF918A8A")
            fontSize: FontSize.PointValue
            fontSizeValue: 5.5
        },
        
        TextStyleDefinition {
            id: titleText
            color: Color.create("#FFFFFFFF")
            fontSize: FontSize.PointValue
            fontSizeValue: 8.0
        },
        TextStyleDefinition {
            id: typeText
            color: Color.create("#FFFFFFFF")
           
            fontSize: FontSize.XSmall
            fontStyle: FontStyle.Italic
        },
        TextStyleDefinition {
            id: lengthStyle
            color: Color.create("#FFFFFFFF")
            
            fontSize: FontSize.XSmall
            fontStyle: FontStyle.Italic
        },
        TextStyleDefinition {
            id: indexStyle
            color: Color.create("#FFFFFFFF")
            
            fontSize: FontSize.XSmall
            fontStyle: FontStyle.Italic
        },
        TextStyleDefinition {
            id: slideDescStyle
            color: Color.create("#FFFFFFFF")
            
            fontSize: FontSize.XSmall
            fontStyle: FontStyle.Italic
        }
    ]
    
    function get(url, callback) {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            if (request.readyState === XMLHttpRequest.DONE) {
                if (callback) {
                    callback({
                            result: request.status === 200,
                            status: request.status,
                            message: request.responseText
                    });
                }
            }
        };
        request.open("GET", url, true);
        request.send();
    }
    
    function post(url, postdata, callback) {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            if (request.readyState === XMLHttpRequest.DONE) {
                if (callback) {
                    callback({
                            result: request.status === 200,
                            status: request.status,
                            message: request.responseText
                    });
                }
            }
        };
        
        request.open("POST", url, true);
        request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        request.send(postdata);
    }
    
    function getIndex(page,index,len){
        var pt = "_000000000000000000000000000000"+page;
        var it = "_000000000000000000000000000000"+index;
        
        return pt.substr(pt.length-len)+"_"+it.substr(it.length-len);
    }
    
    //---------------------------------------------------
    // 日期格式化
    // 格式 YYYY/yyyy/YY/yy 表示年份
    // MM/M 月份
    // W/w 星期
    // dd/DD/d/D 日期
    // hh/HH/h/H 时间
    // mm/m 分钟
    // ss/SS/s/S 秒
    // zz/ZZ/z/Z 毫秒
    //---------------------------------------------------
    function formatDate(formatStr) {
        var myDate = new Date();
        var fullNian = myDate.getFullYear()
        var nian = myDate.getYear()
        var yue = myDate.getMonth() + 1
        var day = myDate.getDate()
        var shi = myDate.getHours()
        var fen = myDate.getMinutes()
        var miao = myDate.getSeconds()
        var hmiao = myDate.getMilliseconds();
        
        var str = formatStr;
        var Week = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
        
        str = str.replace(/yyyy|YYYY/, fullNian);
        str = str.replace(/yy|YY/, (nian % 100) > 9 ? (nian % 100).toString() : '0' + (nian % 100));
        
        str = str.replace(/MM/, yue > 9 ? yue.toString() : '0' + yue);
        str = str.replace(/M/g, yue);
        
        str = str.replace(/w|W/g, Week[myDate.getDay()]);
        
        str = str.replace(/dd|DD/, day > 9 ? day.toString() : '0' + day);
        str = str.replace(/d|D/g, day);
        
        str = str.replace(/hh|HH/, shi > 9 ? shi.toString() : '0' + shi);
        str = str.replace(/h|H/g, shi);
        str = str.replace(/mm/, fen > 9 ? fen.toString() : '0' + fen);
        str = str.replace(/m/g, fen);
        
        str = str.replace(/ss|SS/, miao > 9 ? miao.toString() : '0' + miao);
        str = str.replace(/s|S/g, miao);
        
        str = str.replace(/zzz|ZZZ/g, hmiao);
        
        return str;
    }
    
    function removeHtmlTag(html) {
        var reTag = /<(?:.|\s)*?>/g;
        html = html.replace(reTag, "");
        
        return html;
    }
    
    function removeCode(str) {
        var pattern = new RegExp("[#&]")
        var rs = "";
        for (var i = 0; i < str.length; i ++) {
            rs = rs + str.substr(i, 1).replace(pattern, '');
        }
        
        return rs;
    }
    
    function subStr(str, len) {
        if (str.length > len) {
            return str.substring(0, len) + "..."
        }
        return str;
    }
}
